package com.demo.controller;

import com.demo.service.AccountService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @BelongsProject: demo1
 * @BelongsPackage: com.demo.controller
 * @Author: fwx
 * @CreateTime: 2023-03-01  15:04
 * @Description: TODO
 * @Version: 1.0
 */
@RestController
public class AccountController {
    public static final ReentrantLock Lock=new ReentrantLock();

    @Resource
    private AccountService accountService;

    @PostMapping("/expense")
    public void expenseAccount(String accountNumber,Double cash){
        Lock.lock();
        try {
            accountService.expenseAccount(accountNumber,cash);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            Lock.unlock();
        }
    }

    @PostMapping("/refund")
    public void refundAccount(String accountNumber,Double cash) throws Exception {
        accountService.refundAccount(accountNumber,cash);
    }

    @GetMapping("/getBalance")
    public void getBalance(String accountNumber){
        accountService.getBalance(accountNumber);
    }

}
